#include "pch.h"
#include "Thread.h"


Thread::Thread()
{
    thread_ = CreateThread(NULL, 0, Thread::ThreadFunc, this, CREATE_SUSPENDED, &thread_id_);

    LOG(INFO) << "Create threrad, thread id = " << thread_id_ << ", thread handle = " << thread_;
}


Thread::~Thread()
{
}

bool Thread::run()
{
    if (ResumeThread(thread_) == -1)
    {
        LOG(INFO) << "ResumeThread fails, error code = " << GetLastError();
        return false;
    }

    LOG(INFO) << "Thread run, handler = " << thread_ << ", id = " << thread_id_;

    return true;
}

DWORD __stdcall Thread::ThreadFunc(LPVOID lpPara)
{
    Thread *thread = (Thread *)lpPara;

    if (thread == nullptr)
    {
        LOG(INFO) << "Thread exit, thread id = " << thread->thread_id_;
        return 0;
    }

    while (thread->runLoop())
    {

    }

    LOG(INFO) << "Thread exit, thread id = " << thread->thread_id_;

    return 0;
}
